package satir.command;

import java.util.ArrayList;
import java.util.Scanner;
import satir.SatirGame;

/**
 * The Class EnterPlayerNameCmd.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class EnterPlayerNameCmd implements Command {

    /** The satir game. */
    private SatirGame satirGame;
    /** The index. */
    private int index = 1;

    /**
     * Instantiates a new enter player name command.
     * @param satirGame
     *            the Satir game
     */
    public EnterPlayerNameCmd(final SatirGame satirGame) {
	this.satirGame = satirGame;
    }

    /**
     * Execute to retrieve user input.
     */
    public void execute() {
	String playerName;
	Scanner scanner = new Scanner(System.in);
	ArrayList<String> playerNameRec = new ArrayList<String>();
	boolean isRepeat = false;
	for (int i = 0; i < satirGame.getNumOfPlayer(); i++) {
	    do {
		System.out.println("Player " + index
			+ " please input your name:");
		playerName = scanner.next(); // get the player name from input
		isRepeat = playerNameRec.contains(playerName);
		if (isRepeat) {
		    System.out.println("Player name repeated!");
		}
	    } while (isRepeat);
	    playerNameRec.add(playerName);
	    System.out.println("Hi [" + playerName + "]");
	    satirGame.getPlayers(i).setName(playerName);
	    index++;
	}
    }
}
